package com.atguigu.chaptar1

import java.util

object Test33 {
  /**
   * 3、对数据中的元素按照指定规则进行分组
   * Array("zhangsan shenzhen man","lisi beijing woman","zhaoliu beijing man")
   * 规则: 按照地址进行分组
   * 结果： Map( shenzhen->List("zhangsan shenzhen man" ) , beijing->List( "lisi beijing woman","zhaoliu beijing man" ) )
   */
  def main(args: Array[String]): Unit = {
    val arr = Array("zhangsan shenzhen man","lisi beijing woman","zhaoliu beijing man")
    val func=(x:String)=>{x.split(" ")(0)}
    println(group(arr, _.split(" ")(2)))
  }
  def group(arr:Array[String],func:String=>String)={
    val map = new util.HashMap[String, util.List[String]]()
    for (a<-arr){
      val key = func(a)
      if(map.containsKey(key)){
        val list = map.get(key)
        list.add(a)
      }else{
        val newList = new util.ArrayList[String]()
        newList.add(a)
        map.put(key,newList)
      }
    }
    map
  }
}
